/*
TinyXmlStd http://code.google.com/p/tinyxmlstd/
based on TinyXml, www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)

This software is provided 'as-is', without any express or implied 
warranty. In no event will the authors be held liable for any 
damages arising from the use of this software.

Permission is granted to anyone to use this software for any 
purpose, including commercial applications, and to alter it and 
redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.
*/

#include "tinyxmlstd.h"
#include <vector>

namespace tinyxmlstd
{
	struct SpecialChar
	{
		SpecialChar(const String& s, const Char c)
			: encoded(s)
			, actual(c)
		{
		}
		String encoded;
		Char actual;
	};

	std::vector<SpecialChar> CreateSpecialNodeTypes()
	{
		std::vector<SpecialChar> nt;
		nt.push_back(SpecialChar(_T("&amp;"),  '&'));
		nt.push_back(SpecialChar(_T("&lt;"),   '<'));
		nt.push_back(SpecialChar(_T("&gt;"),   '>'));
		nt.push_back(SpecialChar(_T("&quot;"), '\"'));
		nt.push_back(SpecialChar(_T("&apos;"), '\''));
		return nt;
	}

	std::vector<SpecialChar>& SpecialNodeTypes()
	{
		static std::vector<SpecialChar> nt = CreateSpecialNodeTypes();
		return nt;
	}

	bool GetSpecialChar_fromChar(Char toFind, String* result)
	{
		const std::vector<SpecialChar>& nt = SpecialNodeTypes();
		for(std::size_t i=0; i< nt.size(); i++)
		{
			if( nt[i].actual == toFind )
			{
				*result = nt[i].encoded;
				return true;
			}
		}

		return false;
	}

	bool GetSpecialChar_fromEncoded(const Char* toFind, Char* result, std::size_t* length)
	{
		const std::vector<SpecialChar>& nt = SpecialNodeTypes();
		for(std::size_t i=0; i< nt.size(); i++)
		{
			if( Strncmp(nt[i].encoded.c_str(), toFind, nt[i].encoded.length()) == 0 )
			{
				*result = nt[i].actual;
				*length = nt[i].encoded.length();
				return true;
			}
		}

		return false;
	}
}